<?php

/**
 * Klasa zawierająca formularz do Dodawania nowego brandu
 * 
 * @method __construct
 * @category	Zend_Form
 * @package		forms
 * @author Information Mastery
 *
 */
class Form extends Zend_Form {

    public function __construct($options = null) {
        parent::__construct($options);
        $this->setName('Form');
        $this->setMethod('post');
        $this->setAttrib('enctype', 'multipart/form-data');
        $bUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        
        $po = new JezykPoManager(array('locale' => $_SESSION['Zend_Translate']));
        $trans = $po->getTranslator();
        
        
        $notEmpty = new Zend_Validate_NotEmpty();
        $notEmpty->setMessage($trans->_('Pole wymagane!'), 'isEmpty');
        $notEmpty->setMessage($trans->_('Niepoprawna wartosc!'), 'notEmptyInvalid');

        $validator_mail = new Zend_Validate_EmailAddress();
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressInvalid');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressInvalidFormat');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressInvalidHostname');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressInvalidLocalPart');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressInvalidMxRecord');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressInvalidSegment');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressDotAtom');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressQuotedString');
        $validator_mail->setMessage($trans->_('Wpisz poprawny adres e-mail!'), 'emailAddressLengthExceeded');

        $strLenM = new Zend_Validate_StringLength(array('min' => 1, 'max' => 50));
        $strLenM->setMessage($trans->_('Niepoprawna wartosc!'), 'stringLengthInvalid');
        $strLenM->setMessage($trans->_('Wpisz conajmniej jeden znak!'), 'stringLengthTooShort');
        $strLenM->setMessage($trans->_('Wpisz co najwyzej 50 znakow!'), 'stringLengthTooLong');
        
        $unique = new Zend_Validate_Db_NoRecordExists(array('table'=>'adm_ma_teaser','field' =>'mail' ));
        $unique->setMessage($trans->_('Ten adres juz istnieje w bazie!'), 'noRecordFound');
        $unique->setMessage($trans->_('Ten adres juz istnieje w bazie!'), 'recordFound');


        $email = new Zend_Form_Element_Text(admMaTeaser::cl_mail);
        $email
                ->setLabel($trans->_('Twoj Email: '))
                ->setAttrib('class', 'bff_input')
                ->addValidator($notEmpty, true)
                ->addValidator($strLenM, true)
                ->addValidator($validator_mail, true)
                ->addValidator($unique, true)
                ->setRequired(true)

        ;


        $submit = new Zend_Form_Element_Button('submit');
        $submit
                ->setLabel($trans->_('Zapisz sie!'))
                ->setAttrib('id', 'zapisz')
                ->setAttrib('onclick', 'validatorFormularza(getElementById(\'adm_ma_teaser_mail\').value, \''.$bUrl.'\')')
                ;
        $this->addElements(array($email, $submit));
    }
/**
 * Metoda wyświetla błędy z formularza.
 * @param type $p_msg_tab
 * @param type $p_name
 * @return String
 */
    public function wyswietlErrory($p_msg_tab, $p_name) {

        $string = '';
        $i = 0;
        if (isset($p_msg_tab[$p_name]))
            foreach ($p_msg_tab[$p_name] AS $key => $value) {
                if ($i < 1)
                    $string.=$value;$i++;
            }
        return $string;
    }

}

